package com.hopu.blb_erp.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.blb_erp.entity.TMenu;
import com.hopu.blb_erp.service.TMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Api
@EnableSwagger2
@RestController
public class TMenuController {
    @Autowired
    private TMenuService tMenuService;
    @ApiOperation("查询所有仓库")
    @GetMapping("/TMenus")
    public ResponseEntity<List<TMenu>> findOrdersToHandover(){
        List<TMenu> tMenuList = tMenuService.list();
        return ResponseEntity.ok(tMenuList);
    }

    @ApiOperation("查询所有仓库")
    @GetMapping("/menus")
    public ResponseEntity<List<TMenu>> findAll(){
        List<TMenu> list = tMenuService.list();
        return ResponseEntity.ok(list);
    }
    //


    @ApiOperation("查询所有仓库")
    @GetMapping("/tMenus")
    public ResponseEntity<List<TMenu>> findList(){
        List<TMenu> list = tMenuService.findAllTMenus();
        return ResponseEntity.ok(list);
    }
    @ApiOperation("分页查询仓库")
    @GetMapping("/menus/{current}/{pageSize}")
    public ResponseEntity<Page<TMenu>> findPage(@PathVariable("current")Integer current, @PathVariable("pageSize")Integer pageSize){
        Page<TMenu> tMenuPage = new Page<>(current,pageSize);
        Page<TMenu> page = tMenuService.page(tMenuPage);
        return ResponseEntity.ok(tMenuPage);
    }
    @ApiOperation("分页模糊查询仓库")
    @GetMapping("/menu/{current}/{pageSize}")
    public ResponseEntity<Page<TMenu>> findLimit(@PathVariable("current")Integer current, @PathVariable("pageSize")Integer pageSize,  String wareName,String deteTime){
        QueryWrapper<TMenu> queryWrapper = new QueryWrapper<>();
        if (deteTime.equals("null")){
            queryWrapper.like(StringUtils.isNotBlank(wareName),"ware_name",wareName);
            Page<TMenu> tMenuPage = new Page<>(current, pageSize);
            Page<TMenu> page = tMenuService.page(tMenuPage, queryWrapper);
            return ResponseEntity.ok(page);
        }else{
            queryWrapper.like(StringUtils.isNotBlank(wareName), "ware_name", wareName)
                        .like(StringUtils.isNotBlank(deteTime), "dete_time", deteTime);
            Page<TMenu> tMenuPage = new Page<>(current, pageSize);
            Page<TMenu> page = tMenuService.page(tMenuPage, queryWrapper);
            return ResponseEntity.ok(page);
        }
    }

    @ApiOperation("根据仓库编号得到仓库id")
    @GetMapping("/menu/{wareId}")
    public ResponseEntity<TMenu> findId(@PathVariable String wareId){
        final TMenu one = tMenuService.getOne(new QueryWrapper<TMenu>().lambda().eq(TMenu::getWareNo, wareId));
        return ResponseEntity.ok(one);
    }

    @ApiOperation("添加仓库")
    @PostMapping("/menu")
    public ResponseEntity<String> add(@RequestBody TMenu tMenu){
        LocalDateTime now = LocalDateTime.now();
        Timestamp timestamp = Timestamp.valueOf(now.plusHours(8));
        tMenu.setDeteTime(timestamp);
        tMenuService.save(tMenu);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("删除仓库")
    @DeleteMapping("/menu/{id}")
    public ResponseEntity<String> delete(@PathVariable("id")Integer id){
        tMenuService.removeById(id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("更新仓库")
    @PutMapping("/menu")
    public ResponseEntity<String> modify(@RequestBody TMenu tMenu){
        tMenuService.updateById(tMenu);
        return ResponseEntity.ok("ok");
    }
    @ApiOperation("修改仓库状态")
    @PutMapping("/menu/{id}")
    public ResponseEntity<String> update(@PathVariable("id")Integer id){
        TMenu tMenu = new TMenu();
        tMenu.setId(id);
        tMenuService.modify(tMenu);
        return ResponseEntity.ok("ok");
    }

}
